package org.pada.domain.config.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author shen.peng
 * @since 2024/9/14
 */
@Slf4j
public class RpcConsumerBeanFactory<T> implements FactoryBean<T> {

    private Class<T> clazz;

    private String domain;

    private int mode;

    public RpcConsumerBeanFactory(Class<T> clazz, String domain, int mode) {
        this.clazz = clazz;
        this.domain = domain;
        this.mode = mode;
    }

    @Override
    public T getObject() throws Exception {
        InvocationHandler invocationHandler = mode == 0 ? new RpcConsumerInvokeHandler2(domain):
                new RpcConsumerInvokeHandler(domain);
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, invocationHandler);
    }

    @Override
    public Class<?> getObjectType() {
        return clazz;
    }
}
